/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ez2find.backing;

import com.ez2find.manager.PostManagerLocal;
import com.ez2find.model.Community;
import com.ez2find.model.Event;
import com.ez2find.model.Gig;
import com.ez2find.model.Hous;
import com.ez2find.model.Image;
import com.ez2find.model.Job;
import com.ez2find.model.Personal;
import com.ez2find.model.Post;
import com.ez2find.model.Resume;
import com.ez2find.model.Sale;
import com.ez2find.model.Service;
import com.ez2find.services.EmailLocal;
import com.ez2find.util.SessionContext;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Owner
 */
public class PostBean {

    @EJB
    private PostManagerLocal ejbPostManager;
    @EJB
    private EmailLocal ejbEmail;
    private Post post;
    private int back;
    private long categorieId;
    private String searchValue;
    private String postingType;
    private Date eventDate;
    private boolean forDate;

    public Post getPost() {
        return post;
    }

    public void setPost(Post post) {
        this.post = post;
    }

    public String searchPosting() {
        if (postingType.equals("job")) {
            SessionContext.removeAttribute("jobBean");
            return "listJob";
        }
        if (postingType.equals("resume")) {
            SessionContext.removeAttribute("resumeBean");
            return "listResume";
        }
        if (postingType.equals("house")) {
            SessionContext.removeAttribute("housBean");
            return "listHous";
        }
        if (postingType.equals("sale")) {
            SessionContext.removeAttribute("saleBean");
            return "listSale";
        }
        if (postingType.equals("gig")) {
            SessionContext.removeAttribute("gigBean");
            return "listGig";
        }
        if (postingType.equals("service")) {
            SessionContext.removeAttribute("serviceBean");
            return "listService";
        }
        if (postingType.equals("community")) {
            SessionContext.removeAttribute("communityBean");
            return "listCommunity";
        }
        if (postingType.equals("personal")) {
            SessionContext.removeAttribute("personalBean");
            return "listPersonal";
        }
        if (postingType.equals("event")) {
            SessionContext.removeAttribute("eventBean");
            return "listEvent";
        }
        return null;
    }

    public String listJob() {
        SessionContext.removeAttribute("jobBean");
        return "listJob";
    }

    public String listResume() {
        SessionContext.removeAttribute("resumeBean");
        return "listResume";
    }

    public String listHous() {
        SessionContext.removeAttribute("housBean");
        return "listHous";
    }

    public String listSale() {
        SessionContext.removeAttribute("saleBean");
        return "listSale";
    }

    public String listGig() {
        SessionContext.removeAttribute("gigBean");
        return "listGig";
    }

    public String listService() {
        SessionContext.removeAttribute("serviceBean");
        return "listService";
    }

    public String listCommunity() {
        SessionContext.removeAttribute("communityBean");
        return "listCommunity";
    }

    public String listPersonal() {
        SessionContext.removeAttribute("personalBean");
        return "listPersonal";
    }

    public String listEvent() {
        SessionContext.removeAttribute("eventBean");
        return "listEvent";
    }

    public String createJobPosting() {
        SessionContext.removeAttribute("jobBean");
        return "createJobPosting";
    }

    public String createResumePosting() {
        SessionContext.removeAttribute("resumeBean");
        return "createResumePosting";
    }

    public String createHousPosting() {
        SessionContext.removeAttribute("housBean");
        return "createHousPosting";
    }

    public String createSalePosting() {
        SessionContext.removeAttribute("saleBean");
        return "createSalePosting";
    }

    public String createGigPosting() {
        SessionContext.removeAttribute("gigBean");
        return "createGigPosting";
    }

    public String createServicePosting() {
        SessionContext.removeAttribute("serviceBean");
        return "createServicePosting";
    }

    public String createCommunityPosting() {
        SessionContext.removeAttribute("communityBean");
        return "createCommunityPosting";
    }

    public String createPersonalPosting() {
        SessionContext.removeAttribute("personalBean");
        return "createPersonalPosting";
    }

    public String createEventPosting() {
        SessionContext.removeAttribute("eventBean");
        return "createEventPosting";
    }

    public String evaluationViewPosting() {
        if (post.getType().equals("job")) {
            JobBean jobBean = new JobBean();
            jobBean.setJob((Job) post);
            jobBean.setBack(back);
            SessionContext.setAttribute("jobBean", jobBean);
            return "viewJobPosting";
        }
        if (post.getType().equals("resume")) {
            ResumeBean resumeBean = new ResumeBean();
            resumeBean.setResume((Resume) post);
            resumeBean.setBack(back);
            SessionContext.setAttribute("resumeBean", resumeBean);
            return "viewResumePosting";
        }
        if (post.getType().equals("house")) {
            HousBean housBean = new HousBean();
            housBean.setHous((Hous) post);
            housBean.setBack(back);
            SessionContext.setAttribute("housBean", housBean);
            return "viewHousPosting";
        }
        if (post.getType().equals("sale")) {
            SaleBean saleBean = new SaleBean();
            saleBean.setSale((Sale) post);
            saleBean.setBack(back);
            SessionContext.setAttribute("saleBean", saleBean);
            return "viewSalePosting";
        }
        if (post.getType().equals("gig")) {
            GigBean gigBean = new GigBean();
            gigBean.setGig((Gig) post);
            gigBean.setBack(back);
            SessionContext.setAttribute("gigBean", gigBean);
            return "viewGigPosting";
        }
        if (post.getType().equals("service")) {
            ServiceBean serviceBean = new ServiceBean();
            serviceBean.setService((Service) post);
            serviceBean.setBack(back);
            SessionContext.setAttribute("serviceBean", serviceBean);
            return "viewServicePosting";
        }
        if (post.getType().equals("community")) {
            CommunityBean communityBean = new CommunityBean();
            communityBean.setCommunity((Community) post);
            communityBean.setBack(back);
            SessionContext.setAttribute("communityBean", communityBean);
            return "viewCommunityPosting";
        }
        if (post.getType().equals("personal")) {
            PersonalBean personalBean = new PersonalBean();
            personalBean.setPersonal((Personal) post);
            personalBean.setBack(back);
            SessionContext.setAttribute("personalBean", personalBean);
            return "viewPersonalPosting";
        }
        if (post.getType().equals("event")) {
            EventBean eventBean = new EventBean();
            eventBean.setEvent((Event) post);
            eventBean.setBack(back);
            SessionContext.setAttribute("eventBean", eventBean);
            return "viewEventPosting";
        }
        return null;
    }

    public String evaluationEditPosting() {
        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
        HttpSession session = request.getSession();
        if (session.isNew()) {
            try {
                String errorPageURL = externalContext.getRequestContextPath() + "/faces/welcome.jsp";
                externalContext.redirect(errorPageURL);
                FacesMessage msg = new FacesMessage("The session has bean expired");
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            } catch (IOException ioe) {
                ioe.printStackTrace();
                return null;
            }
        } else {
            if (post.getType().equals("job")) {
                SessionContext.removeAttribute("jobBean");
                SessionBean sessionBean = (SessionBean) SessionContext.getAttribute("sessionBean");
                sessionBean.getEjbSession().setJob((Job) post);
                return "editJobPosting";
            }
            if (post.getType().equals("resume")) {
                SessionContext.removeAttribute("resumeBean");
                SessionBean sessionBean = (SessionBean) SessionContext.getAttribute("sessionBean");
                sessionBean.getEjbSession().setResume((Resume) post);
                return "editResumePosting";
            }
            if (post.getType().equals("house")) {
                SessionContext.removeAttribute("housBean");
                SessionBean sessionBean = (SessionBean) SessionContext.getAttribute("sessionBean");
                sessionBean.getEjbSession().setHous((Hous) post);
                return "editHousPosting";
            }
            if (post.getType().equals("sale")) {
                SessionContext.removeAttribute("saleBean");
                SessionBean sessionBean = (SessionBean) SessionContext.getAttribute("sessionBean");
                sessionBean.getEjbSession().setSale((Sale) post);
                return "editSalePosting";
            }
            if (post.getType().equals("gig")) {
                SessionContext.removeAttribute("gigBean");
                SessionBean sessionBean = (SessionBean) SessionContext.getAttribute("sessionBean");
                sessionBean.getEjbSession().setGig((Gig) post);
                return "editGigPosting";
            }
            if (post.getType().equals("service")) {
                SessionContext.removeAttribute("serviceBean");
                SessionBean sessionBean = (SessionBean) SessionContext.getAttribute("sessionBean");
                sessionBean.getEjbSession().setService((Service) post);
                return "editServicePosting";
            }
            if (post.getType().equals("community")) {
                SessionContext.removeAttribute("communityBean");
                SessionBean sessionBean = (SessionBean) SessionContext.getAttribute("sessionBean");
                sessionBean.getEjbSession().setCommunity((Community) post);
                return "editCommunityPosting";
            }
            if (post.getType().equals("personal")) {
                SessionContext.removeAttribute("personalBean");
                SessionBean sessionBean = (SessionBean) SessionContext.getAttribute("sessionBean");
                sessionBean.getEjbSession().setPersonal((Personal) post);
                return "editPersonalPosting";
            }
            if (post.getType().equals("event")) {
                SessionContext.removeAttribute("eventBean");
                SessionBean sessionBean = (SessionBean) SessionContext.getAttribute("sessionBean");
                sessionBean.getEjbSession().setEvent((Event) post);
                return "editEventPosting";
            }
            return null;
        }
    }

    public String delete() {
        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
        HttpSession session = request.getSession();
        if (session.isNew()) {
            try {
                String errorPageURL = externalContext.getRequestContextPath() + "/faces/welcome.jsp";
                externalContext.redirect(errorPageURL);
                FacesMessage msg = new FacesMessage("The session has bean expired");
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            } catch (IOException ioe) {
                ioe.printStackTrace();
                return null;
            }
        } else {
            try {
                if (post.getTypeReplyTo().equals("masked email")) {
                    ejbEmail.deleteAnonymousEmail(post.getType(), post.getCode(), post.getEmail());
                }
                if (post.getType().equals("house")) {
                    for (Image img : ((Hous) post).getImages()) {
                        File f = new File("D:/Nuevo/ez2find/dist/gfdeploy/site_war/images/" + img.getImage());
                        f.delete();
                    }
                }
                if (post.getType().equals("sale")) {
                    for (Image img : ((Sale) post).getImages()) {
                        File f = new File("D:/Nuevo/ez2find/dist/gfdeploy/site_war/images/" + img.getImage());
                        f.delete();
                    }
                }
                if (post.getType().equals("community")) {
                    for (Image img : ((Community) post).getImages()) {
                        File f = new File("D:/Nuevo/ez2find/dist/gfdeploy/site_war/images/" + img.getImage());
                        f.delete();
                    }
                }
                if (post.getType().equals("personal")) {
                    for (Image img : ((Personal) post).getImages()) {
                        File f = new File("D:/Nuevo/ez2find/dist/gfdeploy/site_war/images/" + img.getImage());
                        f.delete();
                    }
                }
                ejbPostManager.delete(post);
                FacesMessage msg = new FacesMessage("Successful delete your Post");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            } catch (Exception ex) {
                ex.printStackTrace();
                FacesMessage msg = new FacesMessage("Error your Post can't delete");
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            }
        }
    }

    public String toPublish() {
        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
        HttpSession session = request.getSession();
        if (session.isNew()) {
            try {
                String errorPageURL = externalContext.getRequestContextPath() + "/faces/welcome.jsp";
                externalContext.redirect(errorPageURL);
                FacesMessage msg = new FacesMessage("The session has bean expired");
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            } catch (IOException ioe) {
                ioe.printStackTrace();
                return null;
            }
        } else {
            try {
                if (post.getTypeReplyTo().equals("masked email")) {
                    ejbEmail.addAnonymousEmail(post.getType(), post.getCode(), post.getEmail());
                }
                post.setPublishDate(new Date(System.currentTimeMillis()));
                post.setStatus("active");
                ejbPostManager.update(post);
                FacesMessage msg = new FacesMessage("Successful publish your Post");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            } catch (Exception ex) {
                ex.printStackTrace();
                FacesMessage msg = new FacesMessage("Error your Post can't published");
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            }
        }
    }

    public int getBack() {
        return back;
    }

    public void setBack(int back) {
        this.back = back;
    }

    public long getCategorieId() {
        return categorieId;
    }

    public void setCategorieId(long categorieId) {
        this.categorieId = categorieId;
    }

    public Date getEventDate() {
        return eventDate;
    }

    public void setEventDate(Date eventDate) {
        this.eventDate = eventDate;
    }

    public boolean isForDate() {
        return forDate;
    }

    public void setForDate(boolean forDate) {
        this.forDate = forDate;
    }

    public String checkingPostings() {
        try {
            for (Post p : ejbPostManager.listAll()) {
                if (p.getStatus().equals("active")) {
                    if ((p.getPublishDate().getTime() + 1728000000) < System.currentTimeMillis()) {
                        if (p.getTypeReplyTo().equals("masked email")) {
                            ejbEmail.deleteAnonymousEmail(p.getType(), p.getCode(), p.getEmail());
                        }
                        if (p.getType().equals("house")) {
                            for (Image img : ((Hous) p).getImages()) {
                                File f = new File("D:/Nuevo/ez2find/dist/gfdeploy/site_war/images/" + img.getImage());
                                f.delete();
                            }
                        }
                        if (p.getType().equals("sale")) {
                            for (Image img : ((Sale) p).getImages()) {
                                File f = new File("D:/Nuevo/ez2find/dist/gfdeploy/site_war/images/" + img.getImage());
                                f.delete();
                            }
                        }
                        if (p.getType().equals("community")) {
                            for (Image img : ((Community) p).getImages()) {
                                File f = new File("D:/Nuevo/ez2find/dist/gfdeploy/site_war/images/" + img.getImage());
                                f.delete();
                            }
                        }
                        if (p.getType().equals("personal")) {
                            for (Image img : ((Personal) p).getImages()) {
                                File f = new File("D:/Nuevo/ez2find/dist/gfdeploy/site_war/images/" + img.getImage());
                                f.delete();
                            }
                        }
                        ejbPostManager.delete(p);
                    }
                } else if ((p.getCreateDate().getTime() + 604800000) < System.currentTimeMillis()) {
                    if (p.getTypeReplyTo().equals("masked email")) {
                        ejbEmail.deleteAnonymousEmail(p.getType(), p.getCode(), p.getEmail());
                    }
                    if (p.getType().equals("house")) {
                        for (Image img : ((Hous) p).getImages()) {
                            File f = new File("D:/Nuevo/ez2find/dist/gfdeploy/site_war/images/" + img.getImage());
                            f.delete();
                        }
                    }
                    if (p.getType().equals("sale")) {
                        for (Image img : ((Sale) p).getImages()) {
                            File f = new File("D:/Nuevo/ez2find/dist/gfdeploy/site_war/images/" + img.getImage());
                            f.delete();
                        }
                    }
                    if (p.getType().equals("community")) {
                        for (Image img : ((Community) p).getImages()) {
                            File f = new File("D:/Nuevo/ez2find/dist/gfdeploy/site_war/images/" + img.getImage());
                            f.delete();
                        }
                    }
                    if (p.getType().equals("personal")) {
                        for (Image img : ((Personal) p).getImages()) {
                            File f = new File("D:/Nuevo/ez2find/dist/gfdeploy/site_war/images/" + img.getImage());
                            f.delete();
                        }
                    }
                    ejbPostManager.delete(p);
                }
            }
            FacesMessage msg = new FacesMessage("Postings checking succesfully completed");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return null;
        } catch (Exception ex) {
            ex.printStackTrace();
            FacesMessage msg = new FacesMessage("Error checking Postings");
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return null;
        }
    }

    public String getSearchValue() {
        return searchValue;
    }

    public void setSearchValue(String searchValue) {
        this.searchValue = searchValue;
    }

    public String getPostingType() {
        return postingType;
    }

    public void setPostingType(String postingType) {
        this.postingType = postingType;
    }
}
